//商户详细
var urlDetail = server_host + '/mainview/getShopDetail.do';
//可选时间列表
var urlTimeparts = server_host + '/mainview/getKtvTableTimeList.do';
//剩余包厢
var urlRemain = server_host + '/mainview/getTableLastCount.do';
//活动列表
var urlListActs = server_host + '/mainview/getKtvTableActiveList.do';
//评论列表
var urlCommentList = server_host + '/comment/getCommentList.do';

var shopId = getParameterByName('shopId');
var foodInfo = decodeURIComponent(getParameterByName('foodInfo'));

var appModule = angular.module('CheerHiWx', []);
appModule.controller('ShopDetailsCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) {

	$scope.shopId = shopId;
	$scope.foodInfo = foodInfo;

	$scope.getweekday = function(date) {
		var daysOfWeek = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
		return daysOfWeek[date.getDay()];
	}

	//选中的日期&时段
	$scope.selectedTimepart = {
		date: new Date().format('yyyy-mm-dd'),
		weekday: $scope.getweekday(new Date()),
		timepart: '暂无可选时段'
	};

	$scope.selectedTimepartCache = {
		date: new Date().format('yyyy-mm-dd'),
		weekday: $scope.getweekday(new Date()),
		timepart: '暂无可选时段'
	};

	//时段数组，通过日期联动
	$scope.timepartArr = [];

	//加载列表
	$scope.loadList = function() {
		$http.get(urlDetail + '?shopId=' + shopId).success(function(data, status, headers, config) {
			console.log(data);
			$scope.shopDetails = data;
			//TODO
			$timeout(function() {
				$scope.mySwiper = new Swiper('.swiper-container', {
					autoplay: 5000, //可选选项，自动滑动
					loop: true, //可选选项，开启循环
					pagination: '.swiper-pagination'
				});
			}, 10);
			//商户信息存入session
			sessionStorage.setItem('shopInfo', jsonObjToBase64({
				id: $scope.shopDetails.shopId,
				lat: $scope.shopDetails.lat,
				lng: $scope.shopDetails.lng,
				name: $scope.shopDetails.shopName,
				tel: $scope.shopDetails.tel
			}));
		});
	};

	//加载可选时间
	$scope.loadTimeparts = function() {
		$http.get(urlTimeparts + '?shopId=' + shopId).success(function(data, status, headers, config) {
			console.log(data);
			$scope.timeparts = data;
			//默认加载第一天第一个时间段的活动列表
			if ($scope.timeparts.modelList.length > 0) {
				$scope.selectedTimepart.date = $scope.timeparts.modelList[0].date;
				$scope.selectedTimepart.timepart = $scope.timeparts.modelList[0].timeList[0].timePart;
				$scope.timepartArr = $scope.timeparts.modelList[0].timeList;
			}
			$scope.loadActs();
		});
	};

	//加载剩余包厢数
	$scope.loadRemainBoxes = function() {
		$http.get(urlRemain + '?shopId=' + shopId + '&orderDate=' + new Date().format('yyyy-mm-dd')).success(function(data, status, headers, config) {
			console.log(data);
			$scope.remainNum = data;
		});
	};

	//活动列表
	$scope.loadActs = function() {
		$http.get(urlListActs + '?shopId=' + shopId + '&date=' + $scope.selectedTimepart.date + '&timeFlag=true&timePart=' + $scope.selectedTimepart.timepart).success(function(data, status, headers, config) {
			console.log(data);
			$scope.actsList = data;
		});
	};

	// 价格为0的一列不显示----写一个方法!
	$scope.isShow = function(list){
		var price = 0;
		for(var i=0;i<list.length;i++){
			price = price + list[i].price;
		}
		return !!parseInt(price);
	}

	//评论列表
	$scope.loadComments = function() {
		$http.get(urlCommentList + '?shopId=' + shopId + '&page=1').success(function(data, status, headers, config) {
			console.log(data);
			$scope.commentList = data;
		});
	};

	//日期时间段联动
	$scope.renderTimeparts = function() {
		console.log($scope.selectedTimepart.date);
		$scope.timepartArr = findElement($scope.timeparts.modelList, 'date', $scope.selectedTimepartCache.date).timeList;
		$scope.selectedTimepartCache.timepart = '暂无可选时段';
	}

	//时间选择弹出框确认
	$scope.confirmTimepart = function() {
		if ($scope.selectedTimepartCache.timepart == '暂无可选时段')
			alert('请选择时段！');
		else {
			$scope.selectedTimepart.date = $scope.selectedTimepartCache.date;
			$scope.selectedTimepart.timepart = $scope.selectedTimepartCache.timepart;
			$scope.selectedTimepart.weekday = $scope.getweekday(new Date($scope.selectedTimepartCache.date));
			$('#timepartModal').modal('hide');
			$scope.loadActs();
		}
	}

	//点击『预定』
	$scope.orderSubmit = function(priceId) {
		location.href = 'package.html?shopId=' + shopId + '&shopTablePriceId=' + priceId + '&orderDate=' + $scope.selectedTimepart.date;
	}

	// 点击『地图』
	$scope.openmap = function() {
		// location.href = 'map.html?shopId=' + shopId + '&shopName=' + $scope.shopDetails.shopName + '&lat=' + $scope.shopDetails.lat + '&lng=' + $scope.shopDetails.lng; 
		console.log('OPEN MAP--lng:' + $scope.shopDetails.lng + ',lat:' + $scope.shopDetails.lat);
		wx.openLocation({
			latitude: parseFloat($scope.shopDetails.lat), // 纬度，浮点数，范围为90 ~ -90
			longitude: parseFloat($scope.shopDetails.lng), // 经度，浮点数，范围为180 ~ -180。
			name: $scope.shopDetails.shopName, // 位置名
			address: $scope.shopDetails.addr, // 地址详情说明
			scale: 20, // 地图缩放级别,整形值,范围从1~28。默认为最大
			infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
		});
	}

	//预览评论图片
	$scope.previewImg = function(id, _src) {
		var comment = findElement($scope.commentList, 'id', id);
		wx.previewImage({
			current: _src,
			urls: comment.imgList
		});
	}

	// 星星
	$scope.calcStarWidth = function(score) {
		var percent = score / 5;
		var totalLength = 64;
		var starLength = totalLength - (2 / 2) * 4;
		if (score > 0 && score <= 1) {
			return starLength * percent;
		} else if (score > 1 && score <= 2) {
			return starLength * percent + 1;
		} else if (score > 2 && score <= 3) {
			return starLength * percent + 2;
		} else if (score > 3 && score <= 4) {
			return starLength * percent + 3;
		} else if (score > 4 && score <= 5) {
			return starLength * percent + 4;
		}
	}

	// 过滤价格为零
	// $scope.


	console.log('COUNT:' + $('.ng-binding').size());

	$scope.loadList();
	$scope.loadTimeparts();
	$scope.loadRemainBoxes();
	$scope.loadComments();

}]);

// 顶部banner轮播
// var mySwiper = new Swiper('.swiper-container', {
// 	// scrollbar:'.swiper-scrollbar',
// 	// scrollbarHide:false,
// 	autoplay: 5000, //可选选项，自动滑动
// 	loop: true, //可选选项，开启循环
// 	pagination: '.swiper-pagination'
// });

//下拉箭头旋转
$(".banner_ft").click(function() {

	if ($(".moreinfo").is(":hidden")) {
		$("#slidedown_btn").addClass("link");
	} else {
		$("#slidedown_btn").removeClass("link");
	};

	$(".moreinfo").slideToggle("fast");

});

function wxJssdkInit() {
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: server_host + '/h5wx/getWxTicket.do?url=' + encodeURIComponent(location.href.split('#')[0]),
		success: function(data) {
			wx.config({
				debug: false,
				appId: wxAppId,
				timestamp: data.timestamp,
				nonceStr: data.nonceStr,
				signature: data.signature,
				jsApiList: wxJsApi.concat(['previewImage'])
			});
		}
	});
}

if (typeof WeixinJSBridge == "undefined") {
	if (document.addEventListener) {
		document.addEventListener('WeixinJSBridgeReady', wxJssdkInit, false);
	} else if (document.attachEvent) {
		document.attachEvent('WeixinJSBridgeReady', wxJssdkInit);
		document.attachEvent('onWeixinJSBridgeReady', wxJssdkInit);
	}
} else {
	wxJssdkInit();
}